let NumberAnimate = require("../../utils/NumberAnimate");
var app = getApp();

let swiper;
var option = {
  data: {
    num: "",
    dataList: app.globalData.park_list,
    previousIndex: null,
    nextIndex: null,
    current: 0,
    animationMiddleHeaderItem: "",
    animationNavigationItem: "",
    floatOne: false,
    floatTwo: true,
    floatThree: true
  },

  onLoad: function () {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ffac3f',
    })
    wx.setNavigationBarTitle({
      title: '华元车场空位查询',
    })
    if (wx.getStorageSync("flag_index") == true){
      this.setData({
        floatOne: true,
        floatTwo: true,
        floatThree: true        
      })
    } else {
      app.globalData.flag_index = true;
      wx.setStorageSync("flag_index", app.globalData.flag_index);
    }
    var circleCount = 0;
    this.animationMiddleHeaderItem = wx.createAnimation({
      timingFunction: 'linear',
      delay: 100,
      transformOrigin: '50% 50%',
      success: function (res) {
      }
    });
    setInterval(function () {
      if (circleCount % 2 == 0) {
        this.animationMiddleHeaderItem.scale(1.25).step();
      } else {
        this.animationMiddleHeaderItem.scale(1.0).step();
      }
      this.setData({
        animationMiddleHeaderItem: this.animationMiddleHeaderItem.export()
      });
      circleCount++;
      if (circleCount == 1000) {
        circleCount = 0;
      }
    }.bind(this), 1000);

    var circleCount1 = 0;
    this.animationNavigationItem = wx.createAnimation({
      timingFunction: 'linear',
      delay: 100,
      transformOrigin: '50% 50%',
      success: function (res) {
      }
    });
    setInterval(function () {
      if (circleCount % 2 == 0) {
        this.animationNavigationItem.scale(1.1).step();
      } else {
        this.animationNavigationItem.scale(1.0).step();
      }
      this.setData({
        animationNavigationItem: this.animationNavigationItem.export()
      });
      circleCount1++;
      if (circleCount1 == 1000) {
        circleCount1 = 0;
      }
    }.bind(this), 1000);
  this.clcIndex(this.data.current);
  if(app.globalData.onlanch == false)
    this.getParkData();
  },

  goToInfo: function(){
    let url = '../parkInfo/parkInfo?indexview=' + this.data.current;
    console.log(url);
    wx.navigateTo({
      url: url
    });
  },

  goToRoute: function(){
    let url = '../routes/routes?indexview=' + this.data.current;
    wx.navigateTo({
      url: url,
    });
  },

  changeSwiper: function(e) {
    var current = e.detail.current
    this.clcIndex(current);
  },

  clcIndex: function(current) {
    const length = app.globalData.park_list.length;
    this.setData({
      previousIndex: (current + length - 1) % length,
      current: current,
      nextIndex: (current + length + 1) % length
    });
    this.animate(this.data.current);
  },

  animate: function (current) {
    this.setData({
      num: "",
    });
    let num = app.globalData.park_list[current].free;
    let n1 = new NumberAnimate({
      from: num,
      speed: 2000,
      refreshTime: 100,
      onUpdate: () => { 
        this.setData({
          num: n1.tempValue
        });
      },
    });
  },

  onPullDownRefresh: function (){
    var that = this;
    console.log(22222);
    wx.showNavigationBarLoading();  
    that.getParkData();
    console.log(33333);
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  floatOne:function(){
    this.setData({
      floatOne: true,
      floatTwo: false
    })
  },

  floatTwo: function () {
    this.setData({
      floatTwo: true,
      floatThree: false
    })
  },

  floatThree: function () {
    this.setData({
      floatThree: true
    })
  },

  onShareAppMessage: function () {
    return {
      title: '华元车场空位查询',
      desc: '',
      path: 'pages/welcome/welcome',
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  },

  getParkData: function (e) {
    var param = [`${app.globalData.park_list[0].parkId}`, `${app.globalData.park_list[1].parkId}`, `${app.globalData.park_list[2].parkId}`];
    console.log(param);
    const that = this;
    wx.request({
      url: 'https://manager.zjheadway.cn/parking/getExtra?parkId=' + param,
      data: JSON.stringify(param),
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data);
        console.log(app.globalData.park_list.length)
        for (var index = 0; index < app.globalData.park_list.length; index++) {
          app.globalData.park_list[index] = Object.assign(app.globalData.park_list[index], res.data.value[index])
        }
        that.setData({
          dataList: app.globalData.park_list
        });
        app.globalData.onlanch = true;
      }
    })
  }
};

Page(option);